﻿using System;
using System.Collections.Generic;

namespace FileRenamer
{
	public class EFiles : List<eFile>
	{
		public void AddList(List<eFile> list)
		{
			foreach (eFile item in list) this.Add(item);
		}
		public bool ContainsExt(String ext)
		{
			for (int i = 0; i < this.Count; i++)
			{
				if (this[i].Ext == ext) return true;
			}
			return false;
		}
		public bool ContainsValue(int value)
		{
			for (int i = 0; i < this.Count; i++)
			{
				if (this[i].Value == value) return true;
			}
			return false;
		}
		public int IndexOfExt(string ext)
		{
			for (int i = 0; i < this.Count; i++)
			{
				if (this[i].Ext == ext) return i;
			}
			return -1;
		}
		public int IndexOfValue(int value)
		{
			for (int i = 0; i < this.Count; i++)
			{
				if (this[i].Value == value) return i;
			}
			return -1;
		}
		public List<int> IndexesOf(int value)
		{
			List<int> ret = new List<int>();
			for (int i = 0; i < this.Count; i++)
				if (this[i].Value == value) ret.Add(i);
			return ret;
		}
		public void SortValues()
		{
			for (int pass = 1; pass < this.Count; pass++)
				for (int i = 0; i < this.Count - 1; i++)
					if (this[i].Value < this[i + 1].Value)
					{
						eFile hold = this[i];
						this[i] = this[i + 1];
						this[i + 1] = hold;
					}
		}

		public void GroupLike()
		{
			List<List<int>> flagged = new List<List<int>>();
			for (int i = 0; i < this.Count; i++)
			{
				List<int> item = IndexesOf(this[i].Value);
				if (item.Count > 1 && !containsList(flagged, item))
				{
					flagged.Add(item);
				}
			}

			List<eFile> toAdd = new List<eFile>();
			foreach (List<int> child in flagged)
			{
				string ext = "";
				for (int i = 0; i < child.Count; i++)
				{
					ext += this[child[i]].Ext + ",";
				}
				if (ext.EndsWith(",")) ext = ext.Remove(ext.Length - 1);
				toAdd.Add(new eFile(ext, this[child[0]].Value));
			}
			List<int> masterList = new List<int>();
			foreach (List<int> child in flagged)
			{
				foreach (int index in child) masterList.Add(index);
				//int disp = 0;
				//foreach (int index in child)
				//{
				//  this.RemoveAt(index - disp);
				//  ++disp;
				//}
			}
			masterList.Sort();
			masterList.Reverse();
			foreach (int i in masterList) this.RemoveAt(i);
			foreach (eFile add in toAdd) this.Add(add);
		}
		private bool containsList(IEnumerable<List<int>> master, ICollection<int> list)
		{
			foreach (List<int> child in master)
			{
				bool contained = true;
				for (int i = 0; i < child.Count; i++)
				{
					if (!list.Contains(child[i])) contained = false;
				}
				if (contained) return true;
			}
			return false;
		}
	}

	#region eFile
	public class eFile
	{
		public string Ext;
		public int Value;
		public eFile(string extension, int value)
		{
			Ext = extension;
			Value = value;
		}
		public override string ToString()
		{
			return Ext + ": " + Value;
		}
	}
	#endregion
}
